import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

def parse_download_url(href):
    # 设置Chrome驱动和选项
    options = webdriver.EdgeOptions()
    options.add_argument('--headless')  # 无头模式
    driver = webdriver.Edge(options=options)
    driver.get(href)

    # WebDriverWait(driver, 10).until(
    #     EC.element_to_be_clickable((By.CSS_SELECTOR, "#a1 > div > video"))
    # ) 
    time.sleep(12)
    # 解析页面获取视频链接
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    # video_src = soup.find_all('h3',class_='h3')
    # for video in video_src:
    #     print (video)















def main(user_page_url,video_file_path):
    # 设置Chrome驱动和选项
    options = webdriver.EdgeOptions()
    # options.add_argument('--headless')  # 无头模式
    driver = webdriver.Edge(options=options)
    driver.get(user_page_url)

    try:
        # 等待弹窗出现，最多等待10秒
        close_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#cpol > span:nth-child(2)"))
        )

        # close_button.click()
        input("请手动扫码登录，完成后按回车键继续...")

    except Exception as e:
        print(f"No popup found or unable to close it. Error: {e}")

    # 解析页面获取视频链接
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    # lis = soup.find_all('li',class_='wqW3g_Kl WPzYSlFQ OguQAD1e')
    lis = soup.select('ul.clearfix')[0].select('a')
    # print(lis)
    driver.quit()

    for li in lis[::-1]:
        href = "https://y80s.tv"+li.get('href')
        text = li.text
        print(text +":"+ href)
        print("*********************")
        parse_download_url(href)


if __name__ == '__main__':
    loge_file_path = "./parseVideo/log.txt"
    video_file_path = "./parseVideo/"
    video_url = "https://y80s.tv/ju/96039"
    main(video_url,video_file_path)